/*******************************************************************************
 * Copyright (c) 2010 Eugene Vorobkalo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Eugene Vorobkalo - initial API and implementation
 *     Andrey Fedorov - imageView column
 ******************************************************************************/
package org.droidpres.adapter;

//import java.sql.Date;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;
import android.widget.ImageView;
import org.droidpres.R;
import org.droidpres.activity.SetupRootActivity;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.SimpleCursorAdapter.ViewBinder;

public class DocumentListAdapter extends SimpleCursorAdapter implements ViewBinder {
	public static final DateFormat FORMAT_DATE_ISO_8601 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //public static final DateFormat FORMAT_DATE_ONLY = new SimpleDateFormat("yyyy-MM-dd");
    //public static final DateFormat FORMAT_TIME_ONLY = new SimpleDateFormat("HH:mm:ss");

    private Calendar mCl;

	private DecimalFormat cf;
	private DateFormat df;

    //private int state0pic = R.drawable.state0;
    //private int state1pic = R.drawable.state1;
    //private int state2pic = R.drawable.state2;

	private String[] sDocStateInfo;

	public DocumentListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		super(context, layout, c, from, to);
		cf = SetupRootActivity.getCurrencyFormat(context);
		df = SetupRootActivity.getDateFormat(context);
		sDocStateInfo = context.getResources().getStringArray(R.array.DocState);
		setViewBinder(this);
	}

	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		// Очередность columnIndex такая как указано в запросе (создание курсора)
		switch (view.getId()) {
            case R.id.imageView:

                switch(cursor.getInt(columnIndex)){
                    case 0:
                        ((ImageView) view).setImageResource(R.drawable.state0);
                        break;
                    case 1:
                        ((ImageView) view).setImageResource(R.drawable.state1);
                        break;
                    case 2:
                        ((ImageView) view).setImageResource(R.drawable.state2);
                        break;
                    default:
                        break;
                }
                return true;

            case R.id.textDateTime:
                String date;

                //mCl = Calendar.getInstance();
                //java.util.Date parsed_data;

                try {

                    Date parsed_data = FORMAT_DATE_ISO_8601.parse(cursor.getString(columnIndex));

                    //cl.add(Calendar.DAY_OF_MONTH, cur_type_doc.getInt(1));
                    date = df.format(parsed_data);////+"-"+FORMAT_DATE_ONLY.format(Current_Date);

                } catch (ParseException e) {
                    Log.w("_date2",e.getMessage());
                    date = cursor.getString(columnIndex);
                }
                ((TextView) view).setText(date);
                return true;

            case R.id.textSumm:
                ((TextView) view).setText( cf.format(cursor.getDouble(columnIndex)) );
                return true;

		    default:
			    return false;
		}
	}
}